package com.globefrog.framework.web.response;

import lombok.Getter;
import lombok.Setter;
import org.springframework.http.HttpStatus;

/**
 * 基础响应.
 *
 * @author : caijinbang
 * @date : Create in 2021-02-08
 */
@Getter
@Setter
public class BaseResponse<T> {

  public static final String SUCCESS = "success";
  public static final String FAIL = "fail";

  private int code;

  private String message;

  private T data;

  private String stackTrace;

  private Long elapsedTime;

  public BaseResponse() {
  }

  /**
   * 构造函数.
   *
   * @param message 消息
   */
  public BaseResponse(int code, String message) {
    this.code = code;
    this.message = message;
  }

  /**
   * 构造函数.
   *
   * @param code    编码
   * @param message 消息
   * @param data    数据
   */
  public BaseResponse(int code, String message, T data) {
    this.code = code;
    this.message = message;
    this.data = data;
  }

  /**
   * 构造函数.
   *
   * @param code        编码
   * @param message     消息
   * @param elapsedTime 耗时
   */
  public BaseResponse(int code, String message, long elapsedTime) {
    this.code = code;
    this.message = message;
    this.elapsedTime = elapsedTime;
  }


  public static <T> BaseResponse<T> success() {
    return new BaseResponse<T>(HttpStatus.OK.value(), SUCCESS, null);
  }

  public static <T> BaseResponse<T> success(String message) {
    return new BaseResponse<T>(HttpStatus.OK.value(), message, null);
  }

  public static <T> BaseResponse<T> success(T data) {
    return new BaseResponse<T>(HttpStatus.OK.value(), SUCCESS, data);
  }

  public static <T> BaseResponse<T> success(String message, T data) {
    return new BaseResponse<T>(HttpStatus.OK.value(), message, data);
  }

  public static <T> BaseResponse<T> error() {
    return new BaseResponse<T>(HttpStatus.INTERNAL_SERVER_ERROR.value(), FAIL, null);
  }

  public static <T> BaseResponse<T> error(String message) {
    return new BaseResponse<T>(HttpStatus.INTERNAL_SERVER_ERROR.value(), message, null);
  }

  public static <T> BaseResponse<T> error(T data) {
    return new BaseResponse<T>(HttpStatus.INTERNAL_SERVER_ERROR.value(), FAIL, data);
  }

  public static <T> BaseResponse<T> error(String message, T data) {
    return new BaseResponse<T>(HttpStatus.INTERNAL_SERVER_ERROR.value(), message, data);
  }

  public static <T> BaseResponse<T> error(HttpStatus httpStatus, String message) {
    return new BaseResponse<T>(httpStatus.value(), message);
  }

  public static <T> BaseResponse<T> error(HttpStatus httpStatus, String message, long elapsedTime) {
    return new BaseResponse<T>(httpStatus.value(), message, elapsedTime);
  }

  public static <T> BaseResponse<T> badRequest() {
    return new BaseResponse<T>(HttpStatus.BAD_REQUEST.value(), "no identifier arguments", null);
  }

  public static <T> BaseResponse<T> badRequest(String message) {
    return new BaseResponse<T>(HttpStatus.BAD_REQUEST.value(), message, null);
  }

  public static <T> BaseResponse<T> badRequest(T data) {
    return new BaseResponse<T>(HttpStatus.BAD_REQUEST.value(), "no identifier arguments", data);
  }

  public static <T> BaseResponse<T> badRequest(String message, T data) {
    return new BaseResponse<T>(HttpStatus.BAD_REQUEST.value(), message, data);
  }
}
